package ExerChapter14String_;

// 字符串反转
// （1）将字符串转化成char数组;
// （2）交换两个位置，确定零界点是前索引小于后索引
// （3）将char数组转回字符串
// （4）考虑错误的情况
public class Hwork1 {
    public static void main(String[] args) {
        try {
            System.out.println(reverse("alksdjflaksdjlf", 0, 4));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        // StringBuider
        System.out.println(new StringBuilder("abcdefg").substring(0,4).reverse());

    }

    public static String reverse(String str, int start, int end) {
        if (!(str != null && start >= 0 && end <= str.length() && start < end)) {
            throw new RuntimeException("参数不正确！");
        }
        char[] c = str.toCharArray();
        char temp = ' ';
        for (int i = start, j = end; i < j; i++, j--) {
            temp = c[i];
            c[i] = c[j];
            c[j] = temp;
        }
        return new String(c);

    }
}